<template>
	<view class="addres-contanier">
		<view class="addres-list" v-if="list.length">
			<view class="item card-shadow" v-for="(item,index) in list" :key="index" >
				<view class="name-tep">
					<text class="name">{{item.name}}</text>
					<text class="tep">{{item.phone}}</text>
				</view>
				<view class="addres-text" @click="handleEdit(item.addressId)">
					{{item.addressName}}{{item.detail}}
				</view>
				<view class="defaut-delete flex-sb">
					<view class="left" v-if="item.isDefault">
						<text class="label">默认</text>
					</view>
					<view class="right">
						<text class="set-defaut" v-if="!item.isDefault" @click="handleDefault(item)">设为默认</text>
						<text class="set-delete" @click="handleDelete(item.addressId)">删除</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 空 -->
		<unt-empty type="address" v-else></unt-empty>
		<!-- 空 -->
		<!-- 底部按钮 -->
		<view class="addres-fixed">
			<view class="addres-fixed-content">
				<view class="add-addres-btn flex" @click="handleCreate()">
					<u-icon name="plus" color="#fff"></u-icon><text class="text">添加地址</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import * as Addres from "@/api/addres/index.js"
	export default {
		data() {
			return {
				list:[]
			}
		},
		onShow() {
			this.getAddres();
		},
		methods: {
			handleEdit(ids){
				this.$unts.navTo('pages_moon/addres/create/create',{id:ids})
			},
			handleDefault(item){
				const data=Object.assign({},item);
				data.isDefault=!data.isDefault
				Addres.add(data).then(res=>{
					this.getAddres();
				})
			},
			getAddres(){
				Addres.list({}).then(res=>{
					this.list=res.data;
				})
			},
			handleDelete(id){
				Addres.del({ids:id}).then(res=>{
					this.getAddres();
				})
			},
			handleCreate() {
				this.$unts.navTo('pages_moon/addres/create/create')
			},
		}
	}
</script>

<style lang="scss" scoped>
	.addres-contanier {
		position: relative;
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		// 列表
		.addres-list {
			    width: 100%;
			padding: 15rpx;
			margin-bottom: 150rpx;
			.item {
				padding: 25rpx;
				margin-bottom: 30rpx;
				font-size: 28rpx;

				.name-tep {
					font-weight: bold;

					.tep {
						margin-left: 25rpx;
					}
				}

				.addres-text {
					color: #999;
					margin: 15rpx 0px;
					padding-bottom: 15rpx;
					border-bottom: 1px solid #f0f0f4;
				}

				.defaut-delete {
					font-size: 24rpx;

					.left {
						.label {
							padding: 5rpx 10rpx;
							background: #01ab63;
							color: #fff;
							border-radius: 10rpx;
						}
					}

					.right {
						.set-delete {
							margin-left: 25rpx;
							color: #ff461f;
						}
					}

				}
			}
		}

		// 底部按钮
		.addres-fixed {
			position: fixed;
			bottom: var(--window-bottom);
			left: 0;
			right: 0;
			display: flex;
			z-index: 11;
			background: #fff;
			// 设置ios刘海屏底部横线安全区域
			padding-bottom: constant(safe-area-inset-bottom);
			padding-bottom: env(safe-area-inset-bottom);

			.addres-fixed-content {
				width: 100%;
				padding: 10rpx 50rpx;

				.add-addres-btn {
					background: linear-gradient(to right, #01ab63, #09BF09);
					text-align: center;
					color: #fff;
					padding: 20rpx 30rpx;
					border-radius: 200rpx;

					.text {
						margin-left: 10rpx;
					}
				}
			}

		}
	}
</style>
